package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.OrderGoods;
import com.cskaoyan.bean.bo.WxOrderCommentBO;
import com.cskaoyan.bean.bo.WxOrderSubmitBO;
import com.cskaoyan.bean.vo.WxOrderListVO;
import com.cskaoyan.service.wx.WxOrderService;
import org.apache.shiro.SecurityUtils;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    WxOrderService orderService;

    /**
     * /wx/order/list?showType=&page=1&size=10
     * request:showType=1&page=1&size=10
     */
    @RequestMapping("list")
    public BaseRespVo orderList(Integer showType, Integer page, Integer size) {
        Integer userId = (Integer) SecurityUtils.getSubject().getSession().getAttribute("id");
        if (showType == null) {
            showType = 0;
        }
        WxOrderListVO wxOrderListVO = orderService.queryList(showType, page, size, userId);
        return BaseRespVo.ok(wxOrderListVO);
    }

    @RequestMapping("detail")
    public BaseRespVo orderDetail(Integer orderId) {
        Map map = orderService.orderDetail(orderId);
        return BaseRespVo.ok(map);
    }

    @RequestMapping("cancel")
    public BaseRespVo orderCancel(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");
        orderService.orderCancel(orderId);
        return BaseRespVo.ok();
    }

    @PostMapping("refund")
    public BaseRespVo orderRefund(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");
        orderService.orderRefund(orderId);
        return BaseRespVo.ok();
    }

    @PostMapping("delete")
    public BaseRespVo orderDelete(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");
        orderService.orderDelete(orderId);
        return BaseRespVo.ok();
    }

    @PostMapping("confirm")
    public BaseRespVo orderConfirm(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");
        orderService.orderConfirm(orderId);
        return BaseRespVo.ok();
    }

    @RequestMapping("goods")
    public BaseRespVo getOrderGoods(Integer orderId, Integer goodsId) {
        OrderGoods orderGoods = orderService.getOrderGoods(orderId, goodsId);
        return BaseRespVo.ok(orderGoods);
    }

    @PostMapping("comment")
    public BaseRespVo comment(@RequestBody WxOrderCommentBO wxOrderCommentBO) {
        Integer userId = (Integer) SecurityUtils.getSubject().getSession().getAttribute("id");
        orderService.comment(wxOrderCommentBO, userId);
        return BaseRespVo.ok();
    }

    @PostMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");
        orderService.prepay(orderId);
        HashMap<String, Object> result= new HashMap<>();
        result.put("orderId",orderId);
        result.put("status",true);
        return BaseRespVo.ok(result);
    }

    @PostMapping("submit")
    public BaseRespVo submit(@RequestBody WxOrderSubmitBO wxOrderSubmitBO) {
        Integer userId = (Integer) SecurityUtils.getSubject().getSession().getAttribute("id");
        wxOrderSubmitBO.setUserId(userId);
        Map<String, Integer> response = orderService.submit(wxOrderSubmitBO);
        return BaseRespVo.ok(response);
    }
}
